package com.nf.practice.jsonobjcombine;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.io.FileUtils;
import org.junit.jupiter.api.Test;

import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class JsonObjCombine {

    public static void main(String[] args) {
        List<Map<String,String>> list = new ArrayList<>();
        Map<String,String> map1 = new HashMap<>();
        map1.put("name","冯宝宝");
        map1.put("money","123");
        list.add(map1);
        Map<String,String> map2 = new HashMap<>();
        map2.put("name","张楚岚");
        map2.put("money","456");
        list.add(map2);
        Map<String,String> map3 = new HashMap<>();
        map3.put("name","王也");
        map3.put("money","789");
        list.add(map3);
        Map<String,String> map4 = new HashMap<>();
        map4.put("name","张楚岚");
        map4.put("money","100");
        list.add(map4);
        Map<String,String> map5 = new HashMap<>();
        map5.put("name","冯宝宝");
        map5.put("money","200");
        list.add(map5);
        list = list.stream().collect(Collectors.groupingBy(d ->
                d.get("name"))).values().stream().map(e -> {
            Map<String, String> map = e.get(0);
            map.put("money", e.stream().map(f ->
                    new BigDecimal(f.get("money")))
                    .reduce(BigDecimal.ZERO, BigDecimal::add).toString());
            return map;
        }).collect(Collectors.toList());
        System.out.println("分组求和后的list:" + list);
    }


    @Test
    public void test() {
        String s = "12345,678";
        int dex = s.lastIndexOf(",");
        System.out.println(s.substring(0, s.indexOf(",")));
        System.out.println(s.substring(dex+1));
    }


    @Test
    public void test3() {
        String s1 = "1234567";
        s1 = s1.substring(2,4);
        System.out.println(s1);
    }

    @Test
    public void test2() throws IOException {
        File file1 = new File("E:/jsonbaowen/getCreditReportv1");
        File file2= new File("E:/jsonbaowen/xdlCreditReportv1");
        String respJson1 = FileUtils.readFileToString(file1);
        String respJson2 = FileUtils.readFileToString(file2);
        JSONObject resp1 = JSONObject.parseObject(respJson1);
        JSONObject resp2 = JSONObject.parseObject(respJson2);
        JSONObject json1 = resp1.getJSONObject("response");
        JSONObject json2 = resp2.getJSONObject("response");
        json1.putAll(json2);
        json1.put("applyDate", "2022-06-10");
        String s = JSONObject.toJSONString(json1);
        System.out.println(s);
    }


    @Test
    public void test1() {
        JSONObject jsonOne = new JSONObject();
        JSONObject jsonTwo = new JSONObject();
        jsonOne.put("name", "kewen");
        jsonOne.put("age", "24");
        jsonTwo.put("hobbit", "Dota");
        jsonTwo.put("hobbit2", "wow");
        JSONObject jsonThree = new JSONObject();
        jsonThree.putAll(jsonOne);
        jsonThree.putAll(jsonTwo);
        System.out.println(jsonThree.toString());
    }
}
